package com.ztesoft.huangfu.learning.springboot.springbootaop.validator;

import com.ztesoft.huangfu.learning.springboot.springbootaop.validator.constraints.IntegerEnum;
import lombok.Data;
import org.springframework.validation.BindException;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;

import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Author: huangfu.daohui
 * Date: 2018/6/15
 * Time: 14:18
 * Version: 1.0
 * Description:
 **/
@Validated
@Data
public class ValidateTest {
    @NotNull(message = "不能为空")
    private String name;
    @NotNull(message = "不能为空")
    @IntegerEnum(type = {1, 3, 6}, message = "不符合范围")
    private Integer age;

    public static void main(String[] args) {
        ValidateTest validateTest = new ValidateTest();
        validateTest.setAge(2);
        try {
            // 验证所有字段
            ValidateUtils.validate(validateTest);
            // 验证指定字段
//            ValidateUtils.validateProperty(validateTest, "name");

        } catch (BindException e) {
//            e.printStackTrace();
            List list = new ArrayList();
            for (FieldError err : e.getFieldErrors()) {
                System.out.println(err.getField() + "   " + err.getDefaultMessage());
            }
        }
    }
}
